// 导入MySQL模块
let connection = require('../../bin/api');
let configSqlName = require('../../config/sqlname');
var userSQL = require('../../db/BlogSQL');
var multiparty = require('multiparty');
5
var fs = require('fs');

exports.userBlogAllArticle = function (req, res, next) {
    // 从连接池获取连接
    /*    pool.getConnection(function (err, connection) {
     // 获取前台页面传过来的参数
     var param = req.query || req.params;
     // 建立连接 增加一个用户信息
     connection.query(userSQL.queryAll, function (err, result) {
     console.log(result);
     // 释放连接
     connection.release();

     });
     });*/

    //CONN全局变量直接用来增删改查语句
    /* CONN.query(userSQL.queryAll, function (err, result) {
     console.log(result);
     res.json(result);
     // 释放连接
     CONN.release();
     });*/
    connection.SelectData(configSqlName.selectBlogArticleAll, userSQL.blog_article_query_all, function (err, result) {
        res.json(result);
    })
    console.log("啦啦啦");
};


//博客上传
exports.uploadBlogArticle = function (req, res, next) {
    //生成multiparty对象，并配置上传目标路径
    var form = new multiparty.Form({uploadDir: '../../public/manager/img/'});
    //上传完成后处理
    form.parse(req, function (err, fields, files) {
        var filesTmp = JSON.stringify(files, null, 2);

        if (err) {
            console.log('parse error: ' + err);
        } else {
            console.log('parse files: ' + filesTmp);
            var inputFile = files.inputFile[0];
            var uploadedPath = inputFile.path;
            var dstPath = './public/files/' + inputFile.originalFilename;
            //重命名为真实文件名
            fs.rename(uploadedPath, dstPath, function (err) {
                if (err) {
                    console.log('rename error: ' + err);
                } else {
                    console.log('rename ok');
                    console.log('上传文件成功');
                }
            });
        }
        res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'});
        res.write('received upload:\n\n');
    });
};